package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ymatou.iapi.livebuyer.parameter.QueryHotSaleProductsBean;
import com.ymatou.iapi.livebuyer.service.QueryHotSaleProductsCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.prodstock.parameter.PreOrderReduceCatalogDto;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProdStockCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*热卖商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryHotSaleProducts{
	private static QueryHotSaleProductsBean queryhotsaleproductsBean;
	private static QueryHotSaleProductsCall queryhotsaleproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("热卖商品列表");
	}

	@Before
	public void caseUp() {
		queryhotsaleproductsBean=new QueryHotSaleProductsBean();
		queryhotsaleproductsCall=new QueryHotSaleProductsCall();
		LiveRoomWapper =new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryhotsaleproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryHotSaleProducts_001() {
		Logger.start(true,"热卖商品列表成功");
		try {
//			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
//			//获取商品ID
//			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
//			
//			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
//			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid);
			
			//TODO 查询当前有效直播总数量
			List<Map> list = new LiveRoomWapper().queryHotSaleProducts();
			queryhotsaleproductsBean.setMaxNum(5);
			queryhotsaleproductsCall.setData(queryhotsaleproductsBean);
			queryhotsaleproductsCall.callService();			
			Logger.verifyEquals("200", queryhotsaleproductsCall.getString("code"), "验证code");
			JsonArray jd = queryhotsaleproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(queryhotsaleproductsBean.getMaxNum()<list.size()?queryhotsaleproductsBean.getMaxNum():list.size(),jd.size(),"验证数量");
			int i=0;
			for (JsonElement data : jd) {
//				Logger.debug(data.toString());
//				Logger.debug(list.get(i).toString());
				Logger.verifyEquals(data.getAsJsonObject().get("liveId").getAsString(), list.get(i).get("LiveId").toString(), "验证LiveId");
				Logger.verifyEquals(data.getAsJsonObject().get("productId").getAsString(), list.get(i).get("ProductId").toString(), "验证productId");
				i++;
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryHotSaleProducts_002() {
		Logger.start(true,"不输入参数失败");
		try {
			queryhotsaleproductsCall.setData(queryhotsaleproductsBean);
			queryhotsaleproductsCall.callService();			
			Logger.verifyEquals("100", queryhotsaleproductsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryHotSaleProducts_003() {
		Logger.start(true,"个数超过20个失败");
		try {
			queryhotsaleproductsBean.setMaxNum(21);
			queryhotsaleproductsCall.setData(queryhotsaleproductsBean);
			queryhotsaleproductsCall.callService();			
			Logger.verifyEquals("100", queryhotsaleproductsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryHotSaleProducts_004() {
		Logger.start(true,"个数小于0失败");
		try {
			queryhotsaleproductsBean.setMaxNum(-1);
			queryhotsaleproductsCall.setData(queryhotsaleproductsBean);
			queryhotsaleproductsCall.callService();			
			Logger.verifyEquals("100", queryhotsaleproductsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//秒杀商品不上热卖
	public void Tc_QueryHotSaleProducts_005() {
		Logger.start(true,"秒杀热卖商品列表成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			createliveactivityBean.getLiveActivity().setEndTime(String.valueOf(starttime.getTime()+20*60*1000));
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			Logger.comment("直播活动上线");
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Thread.sleep(10000);
			Logger.comment("扣库存");
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(liveacctivityid,cids.get(0),2);
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			Thread.sleep(5000);
			//TODO 查询当前有效直播总数量
			List<Map> list = LiveRoomWapper.queryHotSaleProducts();
			queryhotsaleproductsBean.setMaxNum(10);
			queryhotsaleproductsCall.setData(queryhotsaleproductsBean);
			queryhotsaleproductsCall.callService();			
			Logger.verifyEquals("200", queryhotsaleproductsCall.getString("code"), "验证code");
			JsonArray jd = queryhotsaleproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(queryhotsaleproductsBean.getMaxNum()<list.size()?queryhotsaleproductsBean.getMaxNum():list.size(),jd.size(),"验证数量");
			Logger.verifyIsContains(productId, jd.toString(), "验证热销商品中包含预制数据");
			int i=0;
			for (JsonElement data : jd) {
//				Logger.debug(data.toString());
//				Logger.debug(list.get(i).toString());
				Logger.verifyEquals(data.getAsJsonObject().get("liveId").getAsString(), list.get(i).get("LiveId").toString(), "验证LiveId");
				Logger.verifyEquals(data.getAsJsonObject().get("productId").getAsString(), list.get(i).get("ProductId").toString(), "验证productId");
				if(data.getAsJsonObject().get("productId").getAsString().equals(productId)){
					Logger.verifyEquals(true, data.getAsJsonObject().get("inSecKill").getAsBoolean(), "验证inSecKill");
				}
				i++;
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}